package com.bridgeintelligent.tag.utils;

import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;

/**
 * 该类主要根据动静态标识公共处理不同DimId的后缀拼接
 */
public class FieldHandler {

    public final static String CUST_NO = "cust_id";

    public  static final String ECIF_ID = "user_id";

    public static final String ID_FORMAT = "%s_%s";

    public static final String TRUE = "true";

    public static final String FALSE = "false";

    public static final String SUB_FLAG = "_";

    public static final String ZERO = "0";

    public static final Integer LENGTH8 = 8;

    public static final String CUSTOMER_FLAG = "static_cust_flag";

    public static final Pattern pattern = Pattern.compile("[0-9]*");

    public final static String LAST_MONTH_ONE = "MONTH";

    public final static String LAST_MONTH_TWO = "H01";

    public final static String INDEX_ONE = "1";

    public final static String SPLIT = ",";

    public final static String MOBILE_NUM = "mobile_num";

    public final static String AUDIT = "2";

    public final static String REJECT = "3";

    public final static String PASS = "4";




    /**
     * 超管角色ID
     */
    private final static String UNION_ADMIN = "1FB61720B9C243F2AC97E52B30B454B4";


    public static String addSuffixField(String field, String dataDate, Map<String,String> relationMap){
        if (field.equals(CUST_NO) || field.equals(ECIF_ID)){
            return field;
        }else{
            if (pattern.matcher(dataDate).matches() && dataDate.length() == LENGTH8 ){
                return field;
            }else{
                String esSymbol = Optional.ofNullable(relationMap.get(dataDate)).orElse(dataDate);
                return String.format(ID_FORMAT, field, esSymbol);
            }
        }
    }

    public static String reduceSuffixField(String field,String dataDate){
        if (field.equals(CUST_NO) || field.equals(ECIF_ID)){
            return field;
        }else{
            if (pattern.matcher(dataDate).matches() && dataDate.length() == LENGTH8 ){
                return field;
            }else{
                return StringUtils.substringBeforeLast(field, SUB_FLAG);
            }
        }
    }

    public static boolean isUnionAdmin(String roleStr){
        boolean flag = false;
        if (!StrUtil.isBlank(roleStr) ){
            String[] roleIds = roleStr.split(SPLIT);
            for (String roleId:roleIds) {
                if (StrUtil.equals(roleId,UNION_ADMIN)){
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }

    public static boolean isSentitive(String params){
        if (params.contains(MOBILE_NUM)){
            return true;
        }
        return false;
    }

}
